<?php

declare(strict_types=1);

namespace app\api\controller;

use app\common\model\Learn;
use app\common\tool\Result;
use think\exception\ValidateException;
use app\common\model\Course as CourseModel;
use app\common\model\CourseChapter;
use app\common\model\ExamineUser;
use app\common\model\Examine;
use app\common\model\UserCourse;
use app\common\model\Cate;
use app\common\model\News;

class Course extends Base
{

    /**
     * 课程列表
     */
    public function getList()
    {
        try {
            $param = $this->request->param();
            $cate = getCate($this->user->cate);
            if (empty($cate)) {
                return Result::fail('暂无课程');
            }
            $map[] = ['cate', 'in', $cate];
            $list = CourseModel::getPageList(
                $map,
                'id,title,intro,icon,lecturer',
                $this->getPage(),
                $this->getLimit(),
                false,
            );
            foreach ($list['data'] as &$item) {
                $study = UserCourse::where(['c_id' => $item->id, 'uid' => $this->user->id, 'status' => 2])->count();
                $count = CourseChapter::where('course_id', $item->id)->count();
                $duration = CourseChapter::where('course_id', $item->id)->sum('duration');
                $item->study = $study;
                $item->count = $count;
                if ($count == $study) {
                    $item->is_finish = 1;
                } else {
                    $item->is_finish = 0;
                }
                $item->duration = gmdate("H小时i分钟s秒", intval($duration));
            }
            return Result::fastReturn($list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 课程章节详情
     */
    public function info()
    {
        try {
            $param = $this->request->param();
            $cate = getCate($this->user->cate);
            $item = CourseModel::where('id', $param['id'])->whereIn('cate', $cate)->field('id,title,intro,icon,images,lecturer,lecturer_intro')->find();
            if (empty($item)) {
                return Result::fail('暂无课程');
            }
            $study = UserCourse::where(['c_id' => $item->id, 'uid' => $this->user->id, 'status' => 2])->count();
            $count = CourseChapter::where('course_id', $item->id)->count();
            $duration = CourseChapter::where('course_id', $item->id)->sum('duration');
            $item->study = $study;
            $item->count = $count;
            $item->duration = gmdate("H小时i分钟s秒", intval($duration));
            $list = CourseChapter::where('course_id', $item->id)->field('id,title,course_id')->order('sort asc')->select();
            foreach ($list as &$v) {
                $duration = UserCourse::where(['c_id' => $param['id'], 's_id' => $v->id, 'uid' => $this->user->id])->value('duration');
                if (!empty($duration)) {
                    $v->duration = gmdate("i分钟s秒", intval($duration));
                } else {
                    $v->duration = '';
                }
            }
            $item->list = $list;
            return Result::success('获取成功！', $item);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 获取章节详情
     */
    public function getDetail()
    {
        try {
            $param = $this->request->param();
            $cate = getCate($this->user->cate);
            $info = CourseChapter::where('id', $param['id'])->field('id,title,course_id,icon,video')->find();
            if (empty($info)) {
                return Result::fail('章节不存在');
            }
            $item = CourseModel::where('id', $info['course_id'])->whereIn('cate', $cate)->find();
            if (empty($item)) {
                return Result::fail('暂无课程');
            }
            // $info->video = getVideo($info->id);
            return Result::success('获取成功！', $info);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 添加课程学习记录
     */
    public function studyRecord()
    {
        try {
            $param = $this->request->post();
            return Result::fastReturn(UserCourse::studyRecord($param, $this->user));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 获取首页数据
     */
    public function getIndexList()
    {
        try {
            /**
             * 考试
             */
            $examine_id =  ExamineUser::where(['uid'=>$this->user->id])->column('examine_id');
            $examine = Examine::where([['cate','=',$this->user->cate],['end_time','>=',time()],['id','in',$examine_id]])->field('id,title,score,exam_date,exam_time,start_time,duration,types')->append(['typesText','timeText'])->select();

            /**
             * 课程学习
             */
            $cate = getCate($this->user->cate);
            $map[] = ['cate', 'in', $cate];
            $course = CourseModel::where($map)->limit(2)->field('id,title,icon')->select();
            foreach ($course as &$item) {
                $study = UserCourse::where(['c_id' => $item->id, 'uid' => $this->user->id, 'status' => 2])->count();
                $count = CourseChapter::where('course_id', $item->id)->count();
                $item->study = $study;
                $item->count = $count;
            }
            $courselist=[
                'title'=>$this->user->cateText,
                'list'=>$course
            ];
            /**
             * 学习中心
             */
            $learn = Learn::field('id,title,icon,duration')->order('create_time desc')->limit(4)->select();
            foreach($learn as &$item){
                $item->duration = gmdate("i:s", intval($item->duration));
            }
            /**
             * 院校新闻
             */
            $college_news = News::where(['cate'=>1,'status'=>1])->field('id,title,cate,images,create_time')->order('create_time desc')->limit(4)->select();
            foreach($college_news as &$item){
                $item->create_times = date('Y-m-d',$item->getData('create_time'));
            }
            /**
             * 通知通告
             */
            $notice = News::where(['cate'=>2,'status'=>1])->field('id,title,cate,sub_title')->order('create_time desc')->limit(3)->select();
            $list['examine']=$examine;
            $list['course']=$courselist;
            $list['learn']=$learn;
            $list['college_news']=$college_news;
            $list['notice']=$notice;
            return Result::success('获取成功！', $list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
}
